<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },

    setOptions(chartData = {}) {
      const sideData = chartData.data.map(item => { if (item !== 0) return item })
      this.chart.setOption({
        title: {
          // text: '供水压力',
          text: chartData.text,
          left: 'center',
          top: 10,
          textStyle: {
            color: '#fff',
            fontSize: '16'
          }
        },
        tooltip: {
          trigger: 'axis',
          formatter: '{b} : {c}',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 40,
          left: '6%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: chartData.xAxis,
          // data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
          axisLine: {
            show: true,
            lineStyle: {
              color: 'rgba(255,255,255,.1)',
              width: 1,
              type: 'solid'
            }
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            interval: 0,
            // rotate:50,
            show: true,
            splitNumber: 15,
            textStyle: {
              color: 'rgba(255,255,255,.6)',
              fontSize: '12'
            }
          }
        }],
        yAxis: [{
          type: 'value',

          axisLabel: {
            // formatter: '{value} %'
            show: true,
            textStyle: {
              color: 'rgba(255,255,255,.6)',
              fontSize: '12'
            }
          },
          axisTick: {
            show: false
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: 'rgba(255,255,255,.1)',
              width: 1,
              type: 'solid'
            }
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(255,255,255,.1)'
            }
          },
          min: function (value) {
            let v = value.min / 100
            return Math.floor(v) * 100;
          }
        }],
        series: [{
          name: 'a',
          type: 'bar',
          stack: 'vistors',
          // barWidth: '35%', // 柱子宽度
          // // barGap: 1, //柱子之间间距
          // itemStyle: {
          //   normal: {
          //     color: '#2f89cf',
          //     opacity: 1,
          //     barBorderRadius: 5
          //   }
          // },
          // barWidth: '60%',
          barWidth: 24.5,
          itemStyle: {
            normal: {
              color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [{
                offset: 0,
                color: '#0B4EC3' // 0% 处的颜色
              }, {
                offset: 0.6,
                color: '#138CEB' // 60% 处的颜色
              }, {
                offset: 1,
                color: '#17AAFE' // 100% 处的颜色
              }], false)
            }
          },
          data: chartData.data,
          animationDuration
        },
        {
          type: 'bar',
          barWidth: 8,
          itemStyle: {
            normal: {
              color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [{
                offset: 0,
                color: '#09337C' // 0% 处的颜色
              }, {
                offset: 0.6,
                color: '#0761C0' // 60% 处的颜色
              }, {
                offset: 1,
                color: '#0575DE' // 100% 处的颜色
              }], false)
            }
          },
          barGap: 0,
          data: sideData
        },
        {
          name: 'b',
          tooltip: {
            show: false
          },
          type: 'pictorialBar',
          itemStyle: {
            color: '#1779E0'
          },
          symbol: 'path://M 0,0 l 120,0 l -30,60 l -120,0 z',
          symbolSize: ['31', '4'],
          symbolOffset: ['0', '-3'],
          // symbolRotate: -5,
          symbolPosition: 'end',
          data: chartData.data,
          z: 3
        }]
      })
    }
  }
}
</script>
